import { api } from "@/global/utils/api";
import useApiPromiseHandler from "./api-promise-handler";
import { useI18n } from "vue-i18n" 

export default function usePostModifier(onModifySuccess, onModifyFaild) {
  const { t } = useI18n();

  const handlePostPost = (messageAndResources) => {
    useApiPromiseHandler(t,
      api.post('/posts/create', {
        type: 'images',
        ...messageAndResources
      }),
      onModifySuccess,
      onModifyFaild
    )
  }

  const handleDeletePost = (postId) => {
    useApiPromiseHandler(t,
      api.get(`/posts/delete/${postId}`),
      onModifySuccess, onModifyFaild
    );
  }

  const handleLikePost = (post) => {
    useApiPromiseHandler(t,
      api.get(`/posts/like/${post.id}`),
      () => {post.doseILikedIt = true; post.likeCount++;}, onModifyFaild
    );
  }

  const handleUnlikePost = (post) => {
    useApiPromiseHandler(t,
      api.get(`/posts/unlike/${post.id}`),
      () => {post.doseILikedIt = false; post.likeCount--;}, onModifyFaild
    );
  }
  return { handlePostPost, handleDeletePost, handleLikePost, handleUnlikePost };
}